package edu.nf.vrs.app.advice;

import edu.nf.vrs.app.vo.ResultVO;
import lombok.extern.slf4j.Slf4j;
import org.nf.vrs.authenticate.exception.AuthenticateException;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Author: @Wai
 * Date: 2022-4-12
 */
@ControllerAdvice(basePackages = "edu.nf.vrs.api.controller")
@Slf4j
public class GlobalExceptionHandler {
    
    @ExceptionHandler(AuthenticateException.class)
    public @ResponseBody
    ResultVO handleAuthException(AuthenticateException e) {
        log.error(e.getMessage());
        return response(HttpStatus.UNAUTHORIZED.value(), e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    public @ResponseBody ResultVO handleDataAccessException(RuntimeException e) {
        log.error(e.getMessage());
        return response(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
    }

    private ResultVO response(int code, String message){
        ResultVO vo = new ResultVO();
        vo.setCode(code);
        vo.setMsg(message);
        return vo;
    }
}
